package cn.csc.module.shoppingCard.mq.consumer;

import cn.csc.module.shoppingCard.mq.message.OrderCancelMessage;
import cn.csc.module.shoppingCard.service.card.CardService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.function.Consumer;

import static cn.csc.framework.common.util.json.JsonUtils.toJsonString;

/**
 * 消费订单取消消息
 * orderCancel-in-0
 * @author liuchuang
 * @version 1.0
 * @date 2024/1/30 17:34
 * @modify
 */
@Slf4j
@Configuration
public class OrderCancelConsumer {

    @Autowired
    private CardService cardService;


    /**
     * 订单取消后解锁购物卡
     * message： the input argument
     */
    @Bean
    public Consumer<OrderCancelMessage> orderCancel(){
        return message -> {
            log.info("orderCancelConsumer订单取消开始: {}", toJsonString(message));
            try {
                // 如果订单未支付的话，将订单锁定的购物卡解锁
                cardService.unLockCard(message.getOrderIds(), null);
            } catch (Exception e) {
                log.error("订单取消消息消费失败:{}", message, e);
            }
        };
    }


}
